package com.jhw.ymcc.mq.product;

import com.jhw.ymcc.domain.MediaFile;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;


@Component
public class CourseVideoPushProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public boolean sendMessage(MediaFile mediaFile){
        // destination：配置此消息的topic:tag
        // 1.创建一个message对象，将参数交给他
        Message<MediaFile> message = MessageBuilder.withPayload(mediaFile).build();
        // 2.发送同步消息，得到返回值
        SendResult sendResult = rocketMQTemplate.syncSend("topic-coursevideo:tag-push", message);
        SendStatus sendStatus = sendResult.getSendStatus();
        // 3.判断发送消息是否成功，成功true 失败false
        return SendStatus.SEND_OK == sendStatus?true:false;
    }

}